package com.yidu.erp.service.BeyondDrug;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.common.Tools;
import com.yidu.common.Ware;
import com.yidu.erp.dao.BeyondWareMapper;
import com.yidu.erp.dao.BeyondWaretypeMapper;
import com.yidu.erp.model.BeyondWare;
import com.yidu.erp.model.BeyondWaretype;



@Service
public class DrugServiceImp implements DrugService{

 
	@Resource
	private  BeyondWareMapper  beyondWareMapper;
	@Resource
	private  BeyondWaretypeMapper  beyondWaretypeMapper;

	/**
	 * 查询
	 */
	@Override
	public List<BeyondWare> selectAll(BeyondWare record) {
		//调用查询所有药品的方法
		List<BeyondWare> wareList=beyondWareMapper.selectAll(record);
		//遍历出所有的商品
		for (Iterator iterator = wareList.iterator(); iterator.hasNext();) {
			//强转
			BeyondWare mcWare = (BeyondWare) iterator.next();
			//赋值分类id信息，根据分类id查询分类名称
			mcWare.setTypeId(beyondWaretypeMapper.selectTypeName(mcWare.getTypeId()));
		}
		//返回集合
		return wareList;
	}
	/**
	 * 查询分类信息
	 */
	@Override
	public List<BeyondWaretype> selectType(BeyondWaretype beyondWaretype) {
		List<BeyondWaretype> list=beyondWaretypeMapper.selectType(beyondWaretype);
		return list;
	}
	/**
	 * 显示下拉框商品名称
	 */
	@Override
	public List<BeyondWaretype> selectAllType() {
		List<BeyondWaretype> list=beyondWaretypeMapper.selectAllType();
		return list;
	}
	/**
	 * 根据id查询名称
	 */
	@Override
	public String selectTypeName(String typeid) {

		return beyondWaretypeMapper.selectTypeName(typeid);
	}


		
	@Override
	public BeyondWare selectMes(String wareName) {
		//返回调用根据商品名称查询商品信息的方法
		return beyondWareMapper.selectMes(wareName);
	}
	/**
	 * 查询出商品名称和id
	 */
	@Override
	public List<Ware> selectWare(String query) {
		//新建一个键值集合
		List<Ware> seekList=new ArrayList<Ware>();
		//调用查询商品信息的方法
		List<BeyondWare> list=beyondWareMapper.selectWare(query);
		//遍历出所有的商品名称
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			//强转
			BeyondWare mcWare = (BeyondWare) iterator.next();
			//新建一个键值模型
			Ware seek=new Ware();
			//键值一一对应商品id商品名称赋值
			seek.setValue(mcWare.getWareId());
			seek.setLabel(mcWare.getWareName());
			//加入到集合中
			seekList.add(seek);
		}
		return seekList;
	}
	/**
	 * 根据id删除
	 */
	@Override
	public int deleteByPrimaryKey(String wareId) {
		// TODO Auto-generated method stub
		return beyondWareMapper.deleteByPrimaryKey(wareId);
	}
	/**
	 * 根据分类id删除
	 */
	@Override
	public int deleteTypeByPrimaryKey(String typeId) {
		// TODO Auto-generated method stub
		return beyondWaretypeMapper.deleteByPrimaryKey(typeId);
	}
	/**
	 * 增加
	 */
	@Override
	public int insertSelective(BeyondWaretype record) {
		// TODO Auto-generated method stub
		return beyondWaretypeMapper.insertSelective(record);
	}
	/**
	 * 修改
	 */
	@Override
	public int updateByPrimaryKeySelective(BeyondWaretype typeid) {
		// TODO Auto-generated method stub
		return beyondWaretypeMapper.updateByPrimaryKeySelective(typeid);
	}
	@Override
	public int addOrUpdate(BeyondWaretype record) {
		//定义一个值
		int rows = 0;
		//首先根据字符串判断是否为空
		if(Tools.isEmpty(record.getTypeId())){
			rows=beyondWaretypeMapper.insertSelective(record);
		}else{
			rows=beyondWaretypeMapper.updateByPrimaryKeySelective(record);
		}
		//返回值
		return rows;
	}
}
